
package bibliotheque.server;

import bibliotheque.transaction.TransactionId;
import bibliotheque.wave.WaveAction;
import bibliotheque.wave.WaveAgent;

public class PreValidateAction extends WaveAction {
    
    private TransactionId tid;
    private boolean b;
    
    /** Creates a new instance of PreValidateAction */
    public PreValidateAction(TransactionId tid) {
        this.tid = tid;
        this.b = true;
    }
    
    public void PropagateAfter(WaveAgent agent) throws Exception {
        if (agent instanceof Server) {
            Server server = (Server)agent;
            try {
                server.mutex.acquireUninterruptibly();
                if(server.state == Server.State.SUBSCRIBING) throw new Exception("Validate failed cause server state is subscribing.");
                if(server.state == Server.State.STARTING) throw new Exception("Validate failed cause server state is starting.");
                if(server.state == Server.State.STOPPING) throw new Exception("Validate failed cause server state is stopping.");
                if(server.state == Server.State.STOP) throw new Exception("Validate failed cause server state is stopped.");
                
                server.transaction_manager.PrevalidateInit(this.tid, this.wave_id);
                
            } catch (Exception ex) {
                server.ErrorOccured(ex.getMessage());
                throw ex;
            } finally {
                server.mutex.release();
            }
            
        }
    }
    
    public void Acknowledge(WaveAgent agent) throws Exception {
        Server server ;
        if (agent instanceof Server) {
            server = (Server)agent;
            try {
                server.mutex.acquireUninterruptibly();
                if(server.state == Server.State.SUBSCRIBING) throw new Exception("Free failed cause server state is subscribing.");
                if(server.state == Server.State.STARTING) throw new Exception("Free failed cause server state is starting.");
                if(server.state == Server.State.STOPPING) throw new Exception("Free failed cause server state is stopping.");
                if(server.state == Server.State.STOP) throw new Exception("Free failed cause server state is stopped.");
                
                server.transaction_manager.PreValidateUpdate(this.wave_id, this.b);
                
            } catch (Exception ex) {
                server.ErrorOccured(ex.getMessage());
                throw ex;
            } finally {
                server.mutex.release();
            }
        }
    }
    
    public void LocalEnd(WaveAgent agent) throws Exception {
        Server server ;
        if (agent instanceof Server) {
            server = (Server)agent;
            try {
                server.mutex.acquireUninterruptibly();
                if(server.state == Server.State.SUBSCRIBING) throw new Exception("Free failed cause server state is subscribing.");
                if(server.state == Server.State.STARTING) throw new Exception("Free failed cause server state is starting.");
                if(server.state == Server.State.STOPPING) throw new Exception("Free failed cause server state is stopping.");
                if(server.state == Server.State.STOP) throw new Exception("Free failed cause server state is stopped.");
                
                this.b = server.transaction_manager.PreValidateResult(this.wave_id);
                
            } catch (Exception ex) {
                server.ErrorOccured(ex.getMessage());
                throw ex;
            } finally {
                server.mutex.release();
            }
        }
    }
    
    public void GlobalEnd(WaveAgent agent) throws Exception {
        if (agent instanceof Server) {
            Server server = (Server)agent;
            try {
                server.mutex.acquireUninterruptibly();
                if(server.state == Server.State.SUBSCRIBING) throw new Exception("Selection failed cause server state is subscribing.");
                if(server.state == Server.State.STARTING) throw new Exception("Selection failed cause server state is starting.");
                if(server.state == Server.State.STOPPING) throw new Exception("Selection failed cause server state is stopping.");
                if(server.state == Server.State.STOP) throw new Exception("Selection failed cause server state is stopped.");
                
                if (!this.b){
                    InvalidateAction action = new InvalidateAction(this.tid);
                    server.Initiate(action);
                } else {
                    ValidateAction action = new ValidateAction(this.tid);
                    server.Initiate(action);
                }
                
            } catch (Exception ex) {
                ex.printStackTrace();
                server.ErrorOccured("\nRead error " + ex.getMessage() + "\n");
                throw ex;
            }finally{
                server.mutex.release();
            }
            
        }
    }
    
    
}
